package com.haogu.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.haogu.entity.*;
import com.haogu.service.ContactsService;
import com.haogu.service.impl.CustomerService;
import org.omg.CORBA.INTERNAL;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 联系人控制器
 */
@Controller
@RequestMapping("/contacts")
public class ContactsController {

    @Resource
    private ContactsService contactsService;

    @Resource
    private CustomerService customerService;

    /**
     * 跳联系人主页面
     * @return
     */
    @RequestMapping("/toListContacts")
    public String tolistContacts() {
        return "contacts/list_contacts";
    }

    /**
     * 展示所有联系人
     *
     * @param page
     * @param size
     * @return
     */
    @RequestMapping("/listContacts")
    @ResponseBody
    public Map listContacts(@RequestParam(value = "page", defaultValue = "1") Integer page,
                            @RequestParam(value = "size", defaultValue = "10") Integer size,
                            HttpSession session) {
        //日、周、月新建信息查询
        Object vContacts = session.getAttribute("vContacts");
        if (vContacts != null) {
            Map map = new HashMap<>();
            map.put("result", vContacts);
            Map map1 = new HashMap<>();
            map1.put("data", map);
            session.removeAttribute("vContacts");
            return map1;
        }

        //根据条件模糊查询
        Object vContacts1 = session.getAttribute("vContacts");
        if (vContacts1 != null) {
            Map map = new HashMap<>();
            map.put("result", vContacts1);
            Map map1 = new HashMap<>();
            map1.put("data", map);
            session.removeAttribute("vContacts1");
            return map1;
        }

        //查询所有
        Map map = new HashMap();
        int rows1 = contactsService.rows();
        List<VContacts> list = contactsService.selectContactsLists(page, size);

        map.put("totalRows", rows1);
        map.put("result", list);
        Map map1 = new HashMap();

        map1.put("data", map);
        return map1;
    }

    /**
     * 今日、今周、今月、最近查询
     * @param type
     * @param session
     * @return
     */
    @RequestMapping("/toSelectNewCreateDate")
    public String selectNewCreateDate(@RequestParam("type") String type, HttpSession session) {
        List<VContacts> vContacts = contactsService.todayNewCreateSelect(type);
        if (vContacts != null) {
            session.setAttribute("vContacts", vContacts);
        }
        return "contacts/list_contacts";
    }

    /**
     * 逻辑删除
     *
     * @param id
     * @return
     */
    @RequestMapping("/toDel")
    @ResponseBody
    public ModelMap toDel(String id, HttpSession session) {
        int id1 = Integer.parseInt(id);
        System.out.println();
        int i = contactsService.delCustomers(id1);

        User user = (User) session.getAttribute("user");
        Integer id2 = user.getId();//获取登录者id
        System.out.println();
        contactsService.deleteTime(id2, Integer.valueOf(id));//填充删除时间及删除者的id

        ModelMap m = new ModelMap();
        if (i == 1) {
            m.addAttribute("msg", "删除成功");
            return m;
        }
        m.addAttribute("msg", "删除失败");
        return m;
    }

    /**
     * 根据条件模糊查询
     *
     * @param filterSearch
     * @param search
     * @return
     */
    @RequestMapping("/search")
    public String search(String filterSearch, String search, HttpSession session) {
        List<VContacts> vContacts = contactsService.selectByCustomer(search, filterSearch);
        if (vContacts != null) {
            session.setAttribute("vContacts", vContacts);
        }
        return "contacts/list_contacts";
    }

    /**
     * 发送短信（待定）
     *
     * @return
     */
    @RequestMapping(value = "/chat")
    public String chat(Integer id) {
        System.out.println("短信发送:" + id);
        return "marketing/sendmsg_marketing";
    }

    /**
     * 跳转发送邮件界面
     *
     * @return
     */
    @RequestMapping(value = "/emails")
    public String email(Integer id, HttpSession session, ModelMap modelMap) {
        //获取邮件接收者
        Contacts contacts = contactsService.selectCustomer(id);
        String email1 = contacts.getEmail();
        modelMap.addAttribute("zipcode", email1);
        //获取邮件发送者
        User user = (User) session.getAttribute("user");
        String email = user.getEmail();
        modelMap.addAttribute("email", email);
        return "marketing/sendemail_marketing";
    }

    /**
     * 发送邮件
     *
     * @param zipcode 接收者
     * @param email   发送者
     * @param subject 邮件标题
     * @param content 邮件内容
     * @return
     */
    @RequestMapping("toSendMail")
    @ResponseBody
    public Map sendMail(String zipcode, String email, String subject, String content) {
        contactsService.sendSimpleMail(email, zipcode, subject, content);
        Map map = new HashMap();
        map.put("data", true);
        return map;
    }

    /**
     * 查询添加前的负责人id
     * @return
     */
    @RequestMapping("/cus")
    public String cus(HttpSession session) {
        User user = (User) session.getAttribute("user");
        Integer userId = user.getId();
        session.setAttribute("userId", userId);
        return "contacts/addContacts";
    }

    /**
     * 添加联系人信息
     *
     * @param contacts
     * @return
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public ModelMap save(Contacts contacts, HttpSession session) {
        Integer userId = (Integer) session.getAttribute("userId");
        contacts.setCreator_user_id(userId);
        customerService.insertContacts(contacts);//插入联系人
        ModelMap m = new ModelMap();
        m.addAttribute("msg", "添加成功");
        return m;
    }

    /**
     * 展示联系人某一条信息
     *
     * @return
     */
    @RequestMapping("/sho")
    public String show(Integer id, Model model) {
        Contacts contacts1 = contactsService.selectCustomer(id);
        model.addAttribute("contacts", contacts1);
        return "contacts/show";
    }

    /**
     * 跳修改页面
     * @return
     */
    @RequestMapping("/toUpdate")
    public String toUpdate(Model model,Integer id) {
        Contacts contacts1 = contactsService.selectCustomer(id);
        model.addAttribute("contacts", contacts1);
        return "contacts/change";

    }

    /**
     * 修改
     * @param contacts
     * @return
     */
    @RequestMapping("/change")
    @ResponseBody
    public ModelMap change(Contacts contacts) {
        System.out.println(contacts);
        contactsService.change(contacts);
        ModelMap m = new ModelMap();
        m.addAttribute("msg", "修改成功");
        return m;
    }
}